<!DOCTYPE html>

<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.1</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">

  </head>
  
  <body>
      <form class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
              <div class="layui-inline">
                  <label class="layui-form-label">问题</label>
                  <div class="layui-input-inline">
                      <input type="text" name="title" autocomplete="off" class="layui-input">
                  </div>
              </div>

              <div class="layui-inline">
                  <label class="layui-form-label">类型</label>
                  <div class="layui-input-inline">
                      <select name="typeId" id="bg_id">
                          <option value=""></option>
                      </select>
                  </div>
              </div>

              <div class="layui-inline">
                  <label class="layui-form-label">状态</label>
                  <div class="layui-input-inline">
                      <select name="status" >
                          <option value=""></option>
                          <option value="0">下架</option>
                          <option value="1">上架</option>
                      </select>
                  </div>
              </div>
              <div class="layui-inline">
                  <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                  <button type="reset" class="layui-btn layui-btn-primary" lay-filter="data-search-btn">重置</button>
              </div>
          </div>
      </form>
   
      <!--表格-->
      <table id="demo" lay-filter="test"></table>

    </div>

    <!--toolbar :头部工具栏-->
    <!-- <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
      </div>
    </script> -->

    <!--tool :右侧工具栏-->
    <script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="look">查看</a>
<!--      <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> -->
      <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
    </script>

    <!--开关-->
    <script type="text/html" id="switchTpl">
      <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="上架|下架" lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>
    </script>


			<!-- 时间模板 -->
    <script type="text/html" id="dateTpl">
        {{layui.util.toDateString(d.riseTime, "yyyy-MM-dd") }}
    </script>

<script src="lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="api/baseApi.js" charset="utf-8"></script>
<script th:inline="none">
      layui.use(['laydate','table','form'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
				var $ = layui.$;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });

        //第一个实例
        table.render({
          elem: '#demo'
          ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
          ,defaultToolbar: ['filter', 'exports', 'print']//自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
          ,url: baseUrl + asBaseUrl + 'pageAsList'	//数据接口
          ,page: true //开启分页
          ,limit: 10
          ,limits: [10,20,30]
          ,cols: [[ //表头
            {type: 'numbers', fixed: 'left' }
            ,{field: 'id', title: 'ID', width:100, sort: true, fixed: 'left'}
			,{field: 'nickname', title: '作者昵称', width:120 }
            ,{field: 'questionId', title: '问题ID', width:100}
			,{field: 'introduce', title: '简介', width:300 }
			/*,{field: 'content', title: '回答内容', width:200 }*/
			,{field: 'raiseNum', title: '点赞数', width:80 }
			,{field: 'downNum', title: '点踩数', width:80 }
			,{field: 'commentNum', title: '评论数', width:80 }
			,{field: 'createTime', title: '创建时间', width:160 }
            ,{field: 'publishTime', title: '发布时间', width:160 }
            ,{field: 'status', title: '状态',templet: '#switchTpl', width:100}
            ,{fixed: 'right', align:'center', toolbar: '#barDemo'}
          ]],
             text: {
                    none: '暂无相关数据或权限不够'
            },
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
        });

      // 监听搜索操作
      form.on('submit(data-search-btn)', function (data) {
          var result = JSON.stringify(data.field);
          layer.alert(result, {
              title: '最终的搜索信息'
          });
          //执行搜索重载
          table.reload('demo', {
              page: {
                  curr: 1
              }
              , where: {
                  searchParams: result
              }
          }, 'data');
          return false;
          });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'add':
              var data = checkStatus.data;
              //layer.alert(JSON.stringify(data));
              let index = layer.open({
                title:'添加角色',
                type:2,
                shade:0.2,
                maxmin:true,
                shadeClose:true,
                area:['100%','100%'],
                content:'asAdd',
                yes: function(index, layero){
                  parent.location.reload(); // 父页面刷新
                  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                  parent.layer.close(index);
                }
              })
              break;
            case 'batchDel':
              var data = checkStatus.data;
              layer.msg('选中了：'+ data.length + ' 个');
              break;
                  //自定义头工具栏右侧图标 - 提示
            case 'LAYTABLE_TIPS':
              layer.alert('这是工具栏右侧自定义的一个图标按钮');
              break;
          };
        });

        //监听状态开关操作
        form.on('switch(sexDemo)', function(obj){
          layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
          var id = this.value; // 当行ID
          var status = this.checked ? '1' : '0'; // 选中状态
          var data ={id,status}
          console.log(data)
          $.ajax({
            url: baseUrl + asBaseUrl  ,
            type:'put',
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(data),
            success:function () {
              layer.msg("成功")
            }
          })
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
          var data = obj.data;
          //console.log(obj)
          if(obj.event === 'del'){
            layer.confirm('真的删除行么?', function(index){
              obj.del();
              $.ajax({
                url: baseUrl + answerBaseUrl + 'del/' + obj.data.id,
                type:'delete',
                success:function(res){
                  layer.msg(res.msg)
                  table.reload('demo')
                },
                error:function(res){
                  layer.msg(res.msg)
                }
              })
              layer.close(index);
            });
          } else if(obj.event === 'edit'){
            //将数据存入本地sessionData中
            layui.sessionData('roleData',{
              key:'role'
              ,value:data
            })
            let index = layer.open({
              title:'角色编辑',
              type:2,
              shade:0.2,
              maxmin:true,
              shadeClose:true,
              area:['100%','100%'],
              content:'asEdit',
              yes: function(index, layero){
                parent.location.reload(); // 父页面刷新
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
              }
            })
          }else if(obj.event === 'look'){
              //将数据存入本地sessionData中
              layui.sessionData('asData',{
                  key:'as'
                  ,value:data
              })
              let index = layer.open({
                  title:'回答查看',
                  type:2,
                  shade:0.2,
                  maxmin:true,
                  shadeClose:true,
                  area:['100%','100%'],
                  content:'asLook',
                  yes: function(index, layero){
                      parent.location.reload(); // 父页面刷新
                      var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                      parent.layer.close(index);
                  }
              })
          }
        });



      });
</script>
<script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>
</html>